# -*- coding: UTF-8 -*-

'''
Created on 25.02.2013

@author: Peter He�
http://elinux.org/RPi_config.txt
'''
import unittest
import spreads.CircularSpreadGen as csg
import numpy as np

class Test(unittest.TestCase):

    def feq(self,a,b):
        if abs(a-b)<0.0001:
            return True
        else:
            return False

    def setUp(self):
        pass


    def tearDown(self):
        pass

    def testLenValueGenerationOnDifferentSpreadCounts(self):
        print ("testLenValueGenerationOnDifferentSpreadCounts() " )
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.64])
        self.assertIs(1, len(c.OutputX))
        self.assertIs(1, len(c.OutputY))

        sc = 2
        c.update(Phase_in=[0.64], SpreadCount_in=[sc])
        self.assertIs(sc, len(c.OutputX))
        self.assertIs(sc, len(c.OutputY))

        sc = 15
        c.update(Phase_in=[0.64], SpreadCount_in=[sc])
        self.assertIs(sc, len(c.OutputX))
        self.assertIs(sc, len(c.OutputY))

        sc = 15
        c.update(Phase_in=[0.64, 0.34], SpreadCount_in=[sc])
        self.assertIs(sc*2, len(c.OutputX))
        self.assertIs(sc*2, len(c.OutputY))

        sc = 15
        c.update(InputX_in=[0.34], Phase_in=[0.64, 0.34], SpreadCount_in=[sc])
        self.assertIs(sc*2, len(c.OutputX))
        self.assertIs(sc*2, len(c.OutputY))

        sc = 15
        c.update(InputX_in=[0.34, .34], Phase_in=[0.64, 0.34], SpreadCount_in=[sc])
        self.assertIs(sc*2, len(c.OutputX))
        self.assertIs(sc*2, len(c.OutputY))

        sc = 15
        c.update(InputX_in=[0.34, .34, -45], Phase_in=[0.64, 0.34], SpreadCount_in=[sc])
        self.assertIs(sc*3, len(c.OutputX))
        self.assertIs(sc*3, len(c.OutputY))


        print "testLenValueGenerationOnDifferentSpreadCounts() done"
        pass

       
    def testsomeSimpleValues8(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.5])
        print ("testsomeSimpleValues8() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], -0.5 ))
        self.assertTrue(self.feq(c.OutputY[0], 0.0 ))
        print "testsomeSimpleValues8() done"
        pass
    
    def testsomeSimpleValues9(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.25])
        print ("testsomeSimpleValues9() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], 0.0 ))
        self.assertTrue(self.feq(c.OutputY[0], 0.5 ))
        print "testsomeSimpleValues9() done"
        pass

    def testSpread1(self):
        ex = [0.5000, 0.4045, 0.1545, -0.1545, -0.4045, -0.5000, -0.4045, -0.1545, 0.1545, 0.4045]
        ey = [0.0000, 0.2939, 0.4755, 0.4755, 0.2939, 0.0000, -0.2939, -0.4755, -0.4755, -0.2939]
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[.0, .1, .2, .3, .4, .5, .6, .7, .8, .9])
        print ("testSpread1() ", c.OutputX[0], c.OutputY[0] )
        for i in range(10):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "i=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "i=" + str(i))
        print "testSpread1() done"
        pass

    def testSpread2(self):
        ex = [0.5000, 0.4045, 0.1545, -0.1545, -0.4045, -0.5000, -0.4045, -0.1545, 0.1545, 0.4045]
        ey = [0.0000, 0.2939, 0.4755, 0.4755, 0.2939, 0.0000, -0.2939, -0.4755, -0.4755, -0.2939]
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
        print ("testSpread2() ", c.OutputX[0], c.OutputY[0] )
        for i in range(10):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "i=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "i=" + str(i))
        print "testSpread2() done"
        pass

    def testSpread3(self):
        ex = [0.5000, 0.4045, 0.1545, -0.1545, -0.4045, -0.5000, -0.4045, -0.1545, 0.1545, 0.4045]
        ey = [0.0000, 0.2939, 0.4755, 0.4755, 0.2939, 0.0000, -0.2939, -0.4755, -0.4755, -0.2939]
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[-1.0, -1.1, -1.2, -1.3, -1.4, -1.5, -1.6, -1.7, -1.8, -1.9])
        print ("testSpread3() ", c.OutputX[0], c.OutputY[0] )
        for i in range(10):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "i=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "i=" + str(i))
        print "testSpread3() done"
        pass

    def testSpread4(self):
        ex = [0.5000, 0.4045, 0.1545, -0.1545, -0.4045, -0.5000, -0.4045, -0.1545, 0.1545, 0.4045]
        ex = np.add(ex, 0.1)
        ey = [0.0000, 0.2939, 0.4755, 0.4755, 0.2939, 0.0000, -0.2939, -0.4755, -0.4755, -0.2939]
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[-1.0, -1.1, -1.2, -1.3, -1.4, -1.5, -1.6, -1.7, -1.8, -1.9],
                 InputX_in=[0.1])
        print ("testSpread4() ", c.OutputX[0], c.OutputY[0] )
        for i in range(10):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "i=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "i=" + str(i))
        print "testSpread4() done"
        pass
 
    def testSpread5(self):
        ex = [0.5000, 0.4045, 0.1545, -0.1545, -0.4045, -0.5000, -0.4045, -0.1545, 0.1545, 0.4045]
        ex = np.add(ex, 0.1)
        ey = [0.0000, 0.2939, 0.4755, 0.4755, 0.2939, 0.0000, -0.2939, -0.4755, -0.4755, -0.2939]
        ey = np.add(ey, 0.2)
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[-1.0, -1.1, -1.2, -1.3, -1.4, -1.5, -1.6, -1.7, -1.8, -1.9],
                 InputX_in=[0.1], InputY_in=[0.2])
        print ("testSpread5() ", c.OutputX[0], c.OutputY[0] )
        for i in range(10):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "i=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "i=" + str(i))
        print "testSpread5() done"
        pass

    def testsomeSimpleValues7(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.75])
        print ("testsomeSimpleValues6() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], 0.00 ))
        self.assertTrue(self.feq(c.OutputY[0], -0.5 ))
        print "testsomeSimpleValues6() done"
        pass

    def testsomeSimpleValues6(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.78])
        print ("testsomeSimpleValues6() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], 0.0937 ))
        self.assertTrue(self.feq(c.OutputY[0], -0.4911 ))
        print "testsomeSimpleValues6() done"
        pass

    def testsomeSimpleValues5(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.64])
        print ("testsomeSimpleValues5() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], -0.3187 ))
        self.assertTrue(self.feq(c.OutputY[0], -0.3853 ))
        print "testsomeSimpleValues5() done"
        pass

    def testsomeSimpleValues4(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.44])
        print ("testsomeSimpleValues4() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], -0.4649 ))
        self.assertTrue(self.feq(c.OutputY[0],  0.1841 ))
        print "testsomeSimpleValues4() done"
        pass

    def testsomeSimpleValues3(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.18])
        print ("testsomeSimpleValues3() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0], 0.2129 ))
        self.assertTrue(self.feq(c.OutputY[0], 0.4524 ))
        print "testsomeSimpleValues3() done"
        pass

    def testsomeSimpleValues2(self):
        c = csg.CircularSpread(SpreadCount_in=[1])
        c.update(Phase_in=[0.5])
        print ("testsomeSimpleValues2() ", c.OutputX[0], c.OutputY[0] )
        self.assertTrue(self.feq(c.OutputX[0],-0.5000 ))
        self.assertTrue(self.feq(c.OutputY[0], 0.0000 ))
        print "testsomeSimpleValues2() done"
        pass

    def testsomeSimpleValues1(self):
        #test default
        c = csg.CircularSpread()
        c.update(SpreadCount_in=[1])
        self.assertTrue(self.feq(c.OutputX[0], .5 ))
        self.assertTrue(self.feq(c.OutputY[0], .0 ))
        print "testsomeSimpleValues1() done"
        pass

    def testSpread6(self):
        ex = [ 0.4045, -0.4568, 0.0523, 0.1545, -0.4891, 0.3346, -0.1545, -0.3346, 0.4891 ]
        ey = [ 0.2939, 0.2034, -0.4973, 0.4755, -0.1040, -0.3716, 0.4755, -0.3716, -0.1040 ]
        c = csg.CircularSpread(SpreadCount_in=[3])
        c.update(Phase_in=[.1, .2, .3],
                 InputX_in=[0.0], InputY_in=[0.0])
        print ("testSpread5() ", c.OutputX[0], c.OutputY[0] )
        for i in range(9):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "i=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "i=" + str(i))
        print "testSpread5() done"
        pass

    def testSpreadWithFactorHalf    (self):
        ex = [ 0.4045, -0.0523, -0.4568, 0.1545, -0.3346, -0.4891, -0.1545, -0.4891, -0.3346 ]
        ey = [ 0.2939,  0.4973,  0.2034, 0.4755,  0.3716, -0.1040,  0.4755,  0.1040, -0.3716 ]
        c = csg.CircularSpread(SpreadCount_in=[3])
        c.update(Phase_in=[.1, .2, .3],
                 InputX_in=[0.0], InputY_in=[0.0], Factor_in = [0.5])
        print ("testSpreadWithFactorHalf() ", c.OutputX[0], c.OutputY[0] )
        for i in range(9):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "xi=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "yi=" + str(i))
        print "testSpreadWithFactorHalf() done"
        pass

    def testSpreadWithFactorHalfAndWidthHalf(self):
        ex = [ 0.1618, -0.0209, -0.1827, 0.0618, -0.1338, -0.1956, -0.0618, -0.1956, -0.1338 ]
        ey = [ 0.2939, 0.4973, 0.2034, 0.4755, 0.3716, -0.1040, 0.4755, 0.1040, -0.3716]
        c = csg.CircularSpread(SpreadCount_in=[3])
        c.update(Phase_in=[.1, .2, .3],
                 InputX_in=[0.0], InputY_in=[0.0], Factor_in = [0.5], Width_in=[0.4] )
        print ("testSpreadWithFactorHalfAndWidthHalf() ", c.OutputX[0], c.OutputY[0] )
        for i in range(9):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "xi=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "yi=" + str(i))
        print "testSpreadWithFactorHalfAndWidthHalf() done"
        pass

    def testSpreadWithFactorHalfAndWidthHalfAndHighThird(self):
        ex = [ 0.1618, -0.0209, -0.1827, 0.0618, -0.1338, -0.1956, -0.0618, -0.1956, -0.1338 ]
        ey = [ 0.0970, 0.1641, 0.0671, 0.1569, 0.1226, -0.0343, 0.1569, 0.0343, -0.1226 ]
        c = csg.CircularSpread(SpreadCount_in=[3])
        c.update(Phase_in=[.1, .2, .3],
                 InputX_in=[0.0], InputY_in=[0.0], Factor_in = [0.5], Width_in=[0.4], Height_in=[0.33] )
        print ("testSpreadWithFactorHalfAndWidthHalfAndHighThird() ", c.OutputX[0], c.OutputY[0] )
        for i in range(9):
            print "i=", i,
            self.assertTrue( self.feq(c.OutputX[i], ex[i]), "xi=" + str(i))
            self.assertTrue( self.feq(c.OutputY[i], ey[i]), "yi=" + str(i))
        print "testSpreadWithFactorHalfAndWidthHalfAndHighThird() done"
        pass


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()